package com.duan.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.duan.goods.pojo.Product;
import com.duan.goods.service.ProductService;
import com.duan.goods.mapper.ProductMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author lenovo
* @description 针对表【goods_product(商品表)】的数据库操作Service实现
* @createDate 2024-07-30 11:54:42
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> selectAll() {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getIsDelete,0);
        return productMapper.selectList(queryWrapper);
    }

    @Override
    public Integer insert(Product product) {
        return productMapper.insert(product);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                Product product = new Product();
                product.setId(Long.parseLong(id));
                product.setIsDelete(1);
                productMapper.updateById(product);
            }
            return 1;
        }catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer update(Product product) {
        return productMapper.updateById(product);
    }

    @Override
    public List<Product> selectList(Product product) {
        LambdaQueryWrapper<Product> queryWrapper=this.queryWrapper(product);
        return productMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Product> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Product>) this.selectAll();
    }

    @Override
    public Page<Product> pageList(Integer page, Integer size, Product product) {
        PageHelper.startPage(page,size);
        return (Page<Product>) this.selectList(product);
    }

    @Override
    public Integer audit(Long id, Integer audit) {
        Product product = new Product();
        product.setId(id);
        if (audit == 2) {
            //审核拒绝
            product.setAudit(audit);
            productMapper.updateById(product);
            return 2;
        }else if (audit == 1){
            //审核通过
            product.setAudit(audit);
            product.setStatus(1);
            productMapper.updateById(product);
            return 1;
        }
        return -1;
    }


    @Override
    public Integer status(Long id, Integer status) {
        Product product1 = productMapper.selectById(id);
        if (product1 == null || product1.getIsDelete() == 1) {
            return -1;
        }
        //上架
        if (status == 1) {

            Integer audit = product1.getAudit();
            if (audit != 1) {
                return -1;
            }
        }
        Product product = new Product();
        product.setId(id);
        product.setStatus(status);
        return productMapper.updateById(product);
    }


    public LambdaQueryWrapper<Product> queryWrapper(Product product){
        LambdaQueryWrapper <Product> queryWrapper = new LambdaQueryWrapper<>();
        if (product.getId() != null){
            queryWrapper.eq(Product::getId,product.getId());
        }
        if (product.getName() != null){
            queryWrapper.eq(Product::getName,product.getName());
        }
        if (product.getPrice() != null){
            queryWrapper.eq(Product::getPrice,product.getPrice());
        }
        if (product.getSalesModel() != null){
            queryWrapper.eq(Product::getSalesModel,product.getSalesModel());
        }
        if (product.getType() != null){
            queryWrapper.eq(Product::getType,product.getType());
        }
        if (product.getStatus() != null){
            queryWrapper.eq(Product::getStatus,product.getStatus());
        }
        if (product.getAudit() != null){
            queryWrapper.eq(Product::getAudit,product.getAudit());
        }
        if (product.getShopName() != null){
            queryWrapper.eq(Product::getShopName,product.getShopName());
        }
        if (product.getClassifyId() != null){
            queryWrapper.eq(Product::getClassifyId,product.getClassifyId());
        }
        if (product.getSelling() != null){
            queryWrapper.eq(Product::getSelling,product.getSelling());
        }
        if (product.getParameter() != null){
            queryWrapper.eq(Product::getParameter,product.getParameter());
        }
        if (product.getUnitMeasure() != null){
            queryWrapper.eq(Product::getUnitMeasure,product.getUnitMeasure());
        }
        if (product.getImg() != null){
            queryWrapper.eq(Product::getImg,product.getImg());
        }
        if (product.getVideo() != null){
            queryWrapper.eq(Product::getVideo,product.getVideo());
        }
        if (product.getDescription() != null){
            queryWrapper.eq(Product::getDescription,product.getDescription());
        }
        if (product.getIsDelete() != null){
            queryWrapper.eq(Product::getIsDelete,product.getIsDelete());
        }
        if (product.getCreateTime() != null){
            queryWrapper.eq(Product::getCreateTime,product.getCreateTime());
        }
        if (product.getUpdateTime() != null){
            queryWrapper.eq(Product::getUpdateTime,product.getUpdateTime());
        }
        if (product.getBrandId() != null){
            queryWrapper.eq(Product::getBrandId,product.getBrandId());
        }
        return queryWrapper;
    }
}




